package service

import (
	"gitee.com/sdynasty/clever/codec"
	"fmt"
)

var serviceList map[string]*Service
var routerCodec codec.Codec

func init() {
	serviceList = make(map[string]*Service)
}

func RegisterService(comp...Component) {
	for _, v := range comp {
		s := NewService(v)

		if _, ok := serviceList[s.Name]; ok {
			fmt.Println("service. is already register: ", s.Name)
		}

		if err := s.ExtractHandler(); err != nil {
			fmt.Println("extractHandler error: ", err)
		}
		serviceList[s.Name] = s
		for name, handler := range s.Handlers {
			router := fmt.Sprintf("%s.%s", s.Name, name)
			codec.RegisterMessage(router, handler.Type)
			fmt.Printf("RegisterService router:%s, type:%v\n", router, handler.Type)
		}
	}
}

